package cn.xopencode.recordservice.service.pdroom;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.xopencode.common.framework.exception.util.ServiceExceptionUtil;
import cn.xopencode.common.framework.util.StringUtils;
import cn.xopencode.common.framework.vo.PageResult;
import cn.xopencode.recordservice.convert.pdroom.PdRoomDeptRelConvert;
import cn.xopencode.recordservice.convert.pdroom.PowerDistributionRoomConvert;
import cn.xopencode.recordservice.dal.mysql.dataobject.pdroom.PdRoomDeptRelDO;
import cn.xopencode.recordservice.dal.mysql.dataobject.pdroom.PowerDistributionRoomDO;
import cn.xopencode.recordservice.dal.mysql.mapper.pdroom.PdRoomDeptRelMapper;
import cn.xopencode.recordservice.dal.mysql.mapper.pdroom.PowerDistributionRoomMapper;
import cn.xopencode.recordservice.rpc.pdroom.vo.PowerDistributionRoomPageVO;
import cn.xopencode.recordservice.rpc.pdroom.vo.PowerDistributionRoomUpdateVO;
import cn.xopencode.recordservice.service.pdroom.bo.PowerDistributionRoomBO;
import cn.xopencode.recordservice.service.pdroom.bo.PowerDistributionRoomCreateBO;
import cn.xopencode.recordservice.service.pdroom.bo.PowerDistributionRoomPageBO;
import cn.xopencode.recordservice.service.pdroom.bo.PowerDistributionRoomUpdateBO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static cn.xopencode.recordservice.enums.RecordErrorCodeConstants.RECORD_POWER_ROOM_NOT_EXITS;

/**
 * 配电室管理 service
 */
@Service
@Validated
public class PowerDistributionRoomService {

    @Autowired
    private PowerDistributionRoomMapper powerDistributionRoomMapper;
    @Autowired
    private PdRoomDeptRelMapper pdRoomDeptRelMapper;

    public Integer createPowerDistributionRoom(PowerDistributionRoomCreateBO createBO, Set<String> deptIds){
        PowerDistributionRoomDO roomDO = PowerDistributionRoomConvert.INSTANCE.convert(createBO);
        String roomId = IdUtil.objectId();
        roomDO.setId(roomId);
        Integer result = powerDistributionRoomMapper.insert(roomDO);
        //保存关联的班组信息
        if (!CollectionUtil.isEmpty(deptIds)) {
            List<PdRoomDeptRelDO> pdRoomDeptRels = deptIds.stream()
                    .map(deptId -> new PdRoomDeptRelDO().setId(IdUtil.objectId()).setPdroomId(roomId).setDeptId(deptId)).collect(Collectors.toList());
            pdRoomDeptRelMapper.insertList(pdRoomDeptRels);
        }
        return result;
    }

    public List<PowerDistributionRoomBO> getPowerDistributionRoomList(String roomName){
        List<PowerDistributionRoomDO>  roomDOList = powerDistributionRoomMapper.selectLikeRoomName(roomName);
        List<PowerDistributionRoomBO> roomBOList = PowerDistributionRoomConvert.INSTANCE.convertList(roomDOList);
        return roomBOList;
    }

    public PageResult<PowerDistributionRoomPageVO> pagePowerDistributionRoom(PowerDistributionRoomPageBO pageBO) {
        IPage<PowerDistributionRoomDO> roomDOIPage =  powerDistributionRoomMapper.selectPage(pageBO);
        PageResult<PowerDistributionRoomPageVO> boPageResult = PowerDistributionRoomConvert.INSTANCE.convertPage(roomDOIPage);
        if(!CollectionUtil.isEmpty(boPageResult.getList())){
            List<PowerDistributionRoomPageVO> list = boPageResult.getList();
            for(PowerDistributionRoomPageVO roomVO : list){
                List<PdRoomDeptRelDO> relDOList = pdRoomDeptRelMapper.selectListByPdRoomId(roomVO.getId());
                List<String> deptList = relDOList.stream().map(PdRoomDeptRelDO::getDeptId).collect(Collectors.toList());
                roomVO.setDeptNames(StringUtils.join(deptList, ","));
            }
        }
        return boPageResult;
    }

    public PowerDistributionRoomUpdateVO getPowerDistributionRoomById(String id){
        PowerDistributionRoomDO roomDO = powerDistributionRoomMapper.selectById(id);
        if(roomDO != null){
            PowerDistributionRoomUpdateVO roomVO = PowerDistributionRoomConvert.INSTANCE.convertUpdate(roomDO);
            //查询所有关联的巡检组
            List<PdRoomDeptRelDO> relDOList = pdRoomDeptRelMapper.selectListByPdRoomId(id);
            roomVO.setRelVOList(PdRoomDeptRelConvert.INSTANCE.convertList2(relDOList));
            return roomVO;
        }
        return null;
    }

    public Integer changeStatus(String roomId, Boolean flag){
        PowerDistributionRoomDO roomDO = powerDistributionRoomMapper.selectById(roomId);
        if(roomDO == null){
            throw ServiceExceptionUtil.exception(RECORD_POWER_ROOM_NOT_EXITS);
        }
        roomDO.setStatus(flag);
        return powerDistributionRoomMapper.updateById(roomDO);
    }

    public Integer updatePowerDistributionRoom(PowerDistributionRoomUpdateBO updateBO, Set<String> deptIds){
        PowerDistributionRoomDO roomDO = PowerDistributionRoomConvert.INSTANCE.convert(updateBO);
        Integer result = powerDistributionRoomMapper.updateById(roomDO);
        if(result == null || result != 1){
            throw ServiceExceptionUtil.exception(RECORD_POWER_ROOM_NOT_EXITS);
        }
        //先删除所有关联的班组
        pdRoomDeptRelMapper.deleteByPdroomId(roomDO.getId());

        //保存关联的班组信息
        if (!CollectionUtil.isEmpty(deptIds)) {
            List<PdRoomDeptRelDO> pdRoomDeptRels = deptIds.stream()
                    .map(deptId -> new PdRoomDeptRelDO().setId(IdUtil.objectId()).setPdroomId(roomDO.getId()).setDeptId(deptId)).collect(Collectors.toList());
            pdRoomDeptRelMapper.insertList(pdRoomDeptRels);
        }
        return result;
    }

    public Map<String, String> getlineNameByParam(PowerDistributionRoomBO roomBO) {
        PowerDistributionRoomDO roomDO = powerDistributionRoomMapper.selectOneByParam(roomBO);
        if(roomDO != null){
            Map<String, String> map = new HashMap<>();
            map.put("id", roomDO.getId());
            map.put("roomName", roomDO.getRoomName());
            return map;
        }
        return null;
    }

    public List<PowerDistributionRoomBO> getPdroomNameListByType(Integer recordType){
        List<PowerDistributionRoomDO> roomDOList = powerDistributionRoomMapper.selectPdroomNameListByType(recordType);
        List<PowerDistributionRoomBO> boList = PowerDistributionRoomConvert.INSTANCE.convertList(roomDOList);
        return boList;
    }
}
